package controllers

import (
	"encoding/json"
	"fmt"
	beego "github.com/beego/beego/v2/server/web"
	"news_sys/models"
)

type NewsController struct {
	beego.Controller
}

func (c *NewsController) CreateImage() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Headers", "*")
	if c.Ctx.Input.Method() == "OPTIONS" {
		c.Ctx.Output.SetStatus(200)
		c.StopRun()
	}
	c.Data["json"] = map[string]any{
		"ImageUrl": models.Image(c.GetFile("image")),
	}
	c.ServeJSON()
}
func (c *NewsController) Create() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Headers", "*")
	if c.Ctx.Input.Method() == "OPTIONS" {
		c.Ctx.Output.SetStatus(200)
		c.StopRun()
	}
	var news models.News
	news.NewsStatus = "待审核"
	news.NewsAuthorId, _ = c.GetInt("author_id")
	news.NewsData = c.GetString("data")
	news.NewsTitle = c.GetString("title")
	news.NewsTypeId, _ = c.GetInt("type_id")
	news.NewsTitleImage = c.GetString("image")
	if news.NewsAuthorId == 0 {
		c.Data["json"] = map[string]any{
			"Code": 500,
			"Msg":  "当前账户不可用请登录",
		}
		c.ServeJSON()
		return
	}
	if news.NewsTypeId == 0 {
		c.Data["json"] = map[string]any{
			"Code": 500,
			"Msg":  "当前分类不可用请重新选择",
		}
		c.ServeJSON()
		return
	}
	if news.CreateNews() != nil {
		c.Data["json"] = map[string]any{
			"Code": 500,
			"Msg":  "服务器繁忙",
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = map[string]any{
		"Code": 200,
		"Msg":  "添加成功",
	}
	c.ServeJSON()
}
func (c *NewsController) List() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Headers", "*")
	if c.Ctx.Input.Method() == "OPTIONS" {
		c.Ctx.Output.SetStatus(200)
		c.StopRun()
	}
	page, _ := c.GetInt("page", 1)
	key := fmt.Sprintf("page:%d", page)
	var news models.News
	var NewsList struct {
		News  []models.FromListNews
		Total int
	}
	JsonGet, _ := models.RDB.Get(models.Ctx, key).Result()
	if JsonGet == "" {
		List, Total, err := news.GetNewsList(page)
		NewsList.News = List
		NewsList.Total = Total
		if err != nil {
			c.Data["json"] = map[string]any{
				"Code": 500,
				"Msg":  "服务器繁忙",
			}
			c.ServeJSON()
			return
		}
		c.Data["json"] = map[string]any{
			"Code": 200,
			"Msg":  "获取成功",
			"Data": NewsList,
		}
		c.ServeJSON()
		JsonSet, _ := json.Marshal(&NewsList)
		models.RDB.Set(models.Ctx, key, JsonSet, 0)
	} else {
		err := json.Unmarshal([]byte(JsonGet), &NewsList)
		if err != nil {
			c.Data["json"] = map[string]any{
				"Code": 500,
				"Msg":  "服务器繁忙",
			}
			c.ServeJSON()
			return
		}
		c.Data["json"] = map[string]any{
			"Code": 200,
			"Msg":  "获取成功",
			"Data": NewsList,
		}
		c.ServeJSON()
	}
}
func (c *NewsController) Del() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Headers", "*")
	if c.Ctx.Input.Method() == "OPTIONS" {
		c.Ctx.Output.SetStatus(200)
		c.StopRun()
	}
	var news models.News
	id, _ := c.GetInt("id")
	news.ID = uint(id)
	if news.GetNewsById() != nil {
		c.Data["json"] = map[string]any{
			"Code": 500,
			"Msg":  "服务器繁忙",
		}
		c.ServeJSON()
		return
	}
	if news.NewsStatus == "已发布" {
		c.Data["json"] = map[string]any{
			"Code": 500,
			"Msg":  "已发布的新闻不可删除，请先下架",
		}
		c.ServeJSON()
		return
	}
	if news.DelNewsById() != nil {
		c.Data["json"] = map[string]any{
			"Code": 500,
			"Msg":  "服务器繁忙",
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = map[string]any{
		"Code": 200,
		"Msg":  "删除成功",
	}
	c.ServeJSON()
	return
}
func (c *NewsController) Find() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Headers", "*")
	if c.Ctx.Input.Method() == "OPTIONS" {
		c.Ctx.Output.SetStatus(200)
		c.StopRun()
	}
	id, _ := c.GetInt("id")
	key := fmt.Sprintf("id:%d", id)
	var news models.News
	news.ID = uint(id)
	var News models.FromListNews
	JsonGet, _ := models.RDB.Get(models.Ctx, key).Result()
	if JsonGet == "" {
		var err error
		News, err = news.GetNewsDataById()
		if err != nil {
			c.Data["json"] = map[string]any{
				"Code": 500,
				"Msg":  "服务器繁忙",
			}
			c.ServeJSON()
			return
		}
		c.Data["json"] = map[string]any{
			"Code": 200,
			"Msg":  "获取成功",
			"Data": News,
		}
		c.ServeJSON()
		JsonSet, _ := json.Marshal(&News)
		models.RDB.Set(models.Ctx, key, JsonSet, 0)
	} else {
		err := json.Unmarshal([]byte(JsonGet), &News)
		if err != nil {
			c.Data["json"] = map[string]any{
				"Code": 500,
				"Msg":  "服务器繁忙",
			}
			c.ServeJSON()
			return
		}
		c.Data["json"] = map[string]any{
			"Code": 200,
			"Msg":  "获取成功",
			"Data": News,
		}
		c.ServeJSON()
	}
}
